package com.yuejian_06.yuejian_06.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.yuejian_06.yuejian_06.Listener.LoginListener;
import com.yuejian_06.yuejian_06.R;
import com.yuejian_06.yuejian_06.bean.UserInfoBean;
import com.yuejian_06.yuejian_06.model.LoginModel;

public class LoginActivity extends BaseActivity {
     Button btn_reg;
     Button btn_login;
     EditText et_username,et_password;
     Switch login_switch;
     SharedPreferences sp = null;

    private LoginListener<UserInfoBean> Listener=new LoginListener<UserInfoBean>() {
        @Override
        public void onResponse(UserInfoBean o) {
            UserInfoBean res=o;
            String userid=res.getUser_id();
                if ("0".equals(userid))
                {
                    //处理失败
                    Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();

            }
            else{
                saveUser(userid);//保存userid
                //成功intent跳转到mainactivity中
                Intent intent2= new Intent(LoginActivity.this,MainActivity.class);
                LoginActivity.this.startActivity(intent2);
                Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(),"失败了",Toast.LENGTH_SHORT).show();

        }
    };

    @Override
    void initViews() {
        setLayout(R.layout.activity_login);
        btn_reg=(Button)findViewById(R.id.btn_reg);
        btn_login=(Button)findViewById(R.id.btn_login);
        et_username=(EditText)findViewById(R.id.name);
        et_password=(EditText)findViewById(R.id.pass);
        login_switch=(Switch)findViewById(R.id.login_switch);
        sp = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        if (sp.getBoolean("checkboxBoolean", false))
        {
            et_username.setText(sp.getString("et_username", null));
            et_password.setText(sp.getString("et_password", null));
            login_switch.setChecked(true);

        }
    }

    @Override
    void initEvents() {
        //注册
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent= new Intent();
                intent.setClass(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
            }
        });
        //登录
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = et_username.getText().toString();
                String pswd = et_password.getText().toString();
                if (name.trim().equals("")) {
                    Toast.makeText(LoginActivity.this, "请您输入注册的电话！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (pswd.trim().equals("")) {
                    Toast.makeText(LoginActivity.this, "请您输入密码！", Toast.LENGTH_SHORT).show();
                    return;
                }

                boolean CheckBoxLogin = login_switch.isChecked();
                if (CheckBoxLogin) {
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("et_username", name);
                    editor.putString("et_password", pswd);
                    editor.putBoolean("checkboxBoolean", true);
                    editor.commit();
                } else {
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("et_username", null);
                    editor.putString("et_password", null);
                    editor.putBoolean("checkboxBoolean", false);
                    editor.commit();
                }
                LoginModel loginModel=new LoginModel();
                loginModel.login(et_username.getText().toString(),et_password.getText().toString(),Listener);
            }
        });
    }

    @Override
    void initData() {

    }
    public void saveUser(String user_id)
    {
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor =  sharedPreferences.edit();//获取编辑器
        editor.putString("username",et_username.getText().toString());
        editor.putString("user_id",user_id);
        editor.putString("userpass",et_password.getText().toString());
        editor.commit();//提交修改
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode==1&&resultCode==RESULT_OK){
            String username=data.getStringExtra("username");
            et_username.setText(username);
        }

    }



}
